package org.bozo.im.service.protocols.xmpp.stanza.util.sasl;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.util.encoding.Base64;
import org.bozo.im.service.protocols.xmpp.util.jid.JID;
import org.bozo.im.service.protocols.xmpp.util.namespace.Namespace;

import android.util.Log;

public class PlainMechanism implements Mechanism {

	public String getName() {
		return "PLAIN";
	}

	public void toAuthStanza(XMPPSession pSession, StringBuilder pBuilder) {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.util.sasl.PlainMechanism.toAuthStanza ---------- Authentication through PLAIN mechanism");
		// ---------------------------------------------------------------------------------------
		
		StringBuilder lResponse = new StringBuilder();
		JID lJid = pSession.getConfiguration().getLoginJID();
		
		lResponse.append("\0").append(lJid.getNode()).append("@").append(lJid.getDomain()).append("\0").append(pSession.getConfiguration().getPassword());
		
		String lBase64Response = Base64.encode(lResponse.toString());
		
		pBuilder.append("<auth mechanism='").append(getName()).append("' xmlns='").append(Namespace.SASL.getNamespace()).append("'>");
		pBuilder.append(lBase64Response);
		pBuilder.append("</auth>");
		
		pSession.getConfiguration().putProperty("sasl-mechanism", getName());
	}
}
